package games.eightPuzzle.heuristics;

import games.eightPuzzle.states.*;

/**
 * @author avishl02
 * this class calculate the Manhattan Distances map accordint a given
 * final state
 */
public class MDHelper {
	
	
	/**
	 * this method build the map of distances for MD heuristic
	 * @param numbers
	 * @param finalState
	 * @return
	 */
	public static int[][][] buildMap(int numbers, SlidingPuzzleState finalState){
		
		int[][][] map= new int [finalState.getNumRows()][finalState.getNumCols()][numbers];
		
		CoordinatesHolder ch = new CoordinatesHolder(finalState);
		
		for (int i=0;i<finalState.getNumRows();i++){
			for (int j=0;j<finalState.getNumCols();j++){
				for (int k = 1;k<=numbers;k++){
					if (ch.get(k)==null) continue;
					map[i][j][k-1] = distance(i, j, ch.get(k).getI(),ch.get(k).getJ()); 
				}
			}
		}
		return map;
	}
	
	/**
	 * calculate the distances between 2 given coordinates
	 * @param c1
	 * @param c2
	 * @return
	 */
	public static int distance(Coordinate c1,Coordinate c2){
		return distance(c1.getI(), c1.getJ(), c2.getI(), c2.getJ());
	}
	
	/**
	 * calculate the distances between 2 given coordinates
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 * @return
	 */
	public static int distance(int i1,int j1,int i2,int j2){

		return Math.abs(i1-i2) + Math.abs(j1-j2);
		
	}
	
}
